package com.egao.common.module.cms.controller;

import com.egao.common.core.annotation.OperLog;
import com.egao.common.core.web.JsonResult;
import com.egao.common.core.web.PageParam;
import com.egao.common.core.web.PageResult;
import com.egao.common.module.cms.entity.Question;
import com.egao.common.module.cms.service.QuestionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 常见问题
 * @author pyx
 */
@Api(tags = "常见问题")
@RestController
@RequestMapping("${api}")
public class QuestionController {
    @Autowired
    private QuestionService questionService;

    @PreAuthorize("hasAuthority('cms:question:list')")
    @ApiOperation("分页查询常见问题")
    @OperLog(value = "常见问题管理", desc = "分页查询常见问题")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页", required = false, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "limit", value = "每页条数", required = false, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "title", value = "标题", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "类型", required = true, dataType = "String", paramType = "query"),
    })
    @GetMapping("/cms/question/page")
    public PageResult<Question> page(HttpServletRequest request) {
        PageParam<Question> pageParam = new PageParam<>(request);
        return new PageResult<>(questionService.listPage(pageParam),pageParam.getTotal());
    }

    @PreAuthorize("hasAuthority('cms:question:insert')")
    @ApiOperation("添加常见问题")
    @OperLog(value = "常见问题管理", desc = "添加常见问题", result = true)
    @PostMapping("/cms/question/insert")
    public JsonResult insert(@RequestBody Question question) {
        return questionService.insertQuestion(question);
    }

    @PreAuthorize("hasAuthority('cms:question:update')")
    @ApiOperation("修改常见问题")
    @OperLog(value = "常见问题管理", desc = "修改常见问题", result = true)
    @PutMapping("/cms/question/update")
    public JsonResult update(@RequestBody Question question) {
        return questionService.updateQuestion(question);
    }

    @PreAuthorize("hasAuthority('cms:question:delete')")
    @ApiOperation("删除常见问题")
    @OperLog(value = "常见问题管理", desc = "删除常见问题", result = true)
    @DeleteMapping("/cms/question/delete")
    public JsonResult delete(Integer id) {
        return questionService.deleteQuestion(id);
    }

    @PreAuthorize("hasAuthority('cms:question:detail')")
    @ApiOperation("查询单个常见问题")
    @OperLog(value = "常见问题管理", desc = "查询单个常见问题")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "常见问题id", required = false, dataType = "Integer", paramType = "query"),
    })
    @GetMapping("/cms/question/get")
    public JsonResult listGetId(Integer id) {
        return questionService.getId(id);
    }
}
